package com.superman.supermarket.client.activity;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.google.gwt.event.shared.EventBus;
import com.google.gwt.json.client.JSONArray;
import com.google.gwt.user.client.Window;
import com.google.inject.Inject;
import com.superman.mvpframe.client.ajax.PostManAsyncCallBack;
import com.superman.mvpframe.client.event.ToastEvent;
import com.superman.mvpframe.client.mvp.IPageView;
import com.superman.mvpframe.client.mvp.IPageViewPresenter;
import com.superman.mvpframe.client.utils.XJsonUtil;
import com.superman.supermarket.client.mvp.ShopAbstractActivity;
import com.superman.supermarket.client.view.comment.CommentView;
import com.superman.supermarket.client.view.comment.CommentView.CommentViewPresenter;

/**
 * 评价页面
 * 
 * @author superman
 * @version 2018年8月15日下午5:23:44
 */
public class CommentActivity extends ShopAbstractActivity implements CommentViewPresenter {

	@Inject
	CommentView commentView;
	private String orderid;

	private void getOrderDetail() {
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("ORDER_ID", orderid);
		postman.post("order/query2", map, new PostManAsyncCallBack() {

			@Override
			public void onBusinessError(String statusCode) {

			}

			@Override
			public void onBusinessOK(String response) {
				Map<String, Object> map = XJsonUtil.getBody(response);
				parseToCommentSkuList(map);
			}
		});

	}

	@Override
	public IPageView getView() {
		return commentView;
	}

	@Override
	public IPageViewPresenter getViewPresenter() {
		return this;
	}

	@Override
	public void onActivity(EventBus eventBus) {
		// TODO Auto-generated method stub

	}

	private void parseToCommentSkuList(Map<String, Object> map) {
		List<Map<String, Object>> skuMapList = new ArrayList<Map<String, Object>>();

		JSONArray storeArray = (JSONArray) map.get("STORE_ORDER");
		for (int i = 0; i < storeArray.size(); i++) {
			JSONArray skuArray = storeArray.get(i).isObject().get("SKU_LIST").isArray();
			skuMapList.addAll(XJsonUtil.getList(skuArray));
		}

		commentView.setToCommentSkuList(skuMapList);

	}

	@Override
	public void popToRefresh() {
		// TODO Auto-generated method stub

	}

	@Override
	public void popToRefresh(Object contextData) {
		// TODO Auto-generated method stub

	}

	@Override
	public void pushToRefresh() {
		orderid = Window.Location.getParameter("orderid");
		getOrderDetail();
	}

	@Override
	public void submit(List<Map<String, Object>> list) {
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("ORDER_ID", orderid);
		map.put("SKU_COMMENT", list);
		postman.post("goods/comment/add", map, new PostManAsyncCallBack() {

			@Override
			public void onBusinessError(String statusCode) {
				// TODO Auto-generated method stub

			}

			@Override
			public void onBusinessOK(String response) {
				eventBus.fireEvent(new ToastEvent("评价成功"));

				Map<String, Object> map = new HashMap<String, Object>();
				map.put("orderid", orderid);
				placeController.back(map);
			}
		});
	}

}
